#include"DB.h"

DBFacade* dbfacade = new DBFacade();

Json::Value DBFacade::getMapper(Json::Value query) //寻找bean，插入record
{

	if (query["queryType"].asString()== "0") { //获取详单
		std::string roomIDStr = query["roomId"].asString();
		int newID = atoi(roomIDStr.c_str());
		return getDetailList(newID);
	}
	else if (query["queryType"].asString() == "1") { //获取账单
		std::string roomIDStr = query["roomId"].asString();
		int newID = atoi(roomIDStr.c_str());
		std::cout << newID << "---------------" << std::endl;
		return getpayment(newID);
	}
	else if (query["queryType"].asString() == "3") {//获取报表
		std::string startTimeStr = query["startTime"].asString();
		std::string endTimeStr = query["endTime"].asString();
		time_t sTime = (time_t)atoi(startTimeStr.c_str());
		time_t eTime = (time_t)atoi(endTimeStr.c_str());
		return getStatement(sTime, eTime);
	}
	else if (query["queryType"].asString() == "7") {//登录
		AuthenticationBean myAuth;
		return myAuth.Query(query);

	}
	else if (query["queryType"].asString() == "5") {//获取房间信息
		Json::Value info = RoomResponse(query);
		//if (info["roomId"].asInt() != -1)
			return info;
	}
	else { //消息错误
		std::cout << "消息传错" << std::endl;
		exit(-1);
	}
}
/*
bool DBFacade::getMapper(Room rd) {

}
bool DBFacade::getMapper(user rd) {

}
*/
DBFacade::DBFacade() {
	DBFacade::paymentNumber = 0;
	DBFacade::detailNumber = 0;
	//this->RoomScan(this->to_schedule_list);
}

DBFacade::DBFacade(int diff) {
}

int DBFacade::getpaymentNumber(void) {
	return paymentNumber;
}
int DBFacade::getdetailNumber(void) {
	return detailNumber;
}
void DBFacade::adddetailNumber(void) {
	++(detailNumber);
}
void DBFacade::addpaymentNumber(void) {
	++(paymentNumber);
}

void DBFacade::insertRecord(Record *rd) { //插入record
	RecordBean* rb = new RecordBean(); //建立和数据库的连接
	rb->insert(rd);
	rb->closeDB(); //关闭与数据库的连接
}
double DBFacade::getRoomBill(int roomId) { //算房间费用
	RecordBean* rb = new RecordBean(); //建立和数据库的连接
	std::vector<reqPayment*> req = rb->query1(roomId);
	PaymentList* tmp = new PaymentList(roomId, -1, req);
	return tmp->gettotalFee();
}

Json::Value DBFacade::getDetailList(int newId) //找详单
{
	//获得详单
	RecordBean* rb = new RecordBean();
	std::vector<reqDetail*>result;
	result = rb->query2(newId);
	DetailList* DL = new DetailList(newId, detailNumber++, result); //获得详单

	//打包json
	Json::Value manip_t;
	Json::StreamWriterBuilder wbuilder;
	wbuilder.settings_["indentation"] = "\t";
	manip_t["time"] = DL->gettime();
	manip_t["type"] = (int)DL->gettype();
	manip_t["response_type"] = (int)DL->getresponseType();
	manip_t["roomId"] = DL->getroomId();
	manip_t["listId"] = DL->getlistId();
	std::vector<std::string> infoStr = DL->getinfo();
	std::string addStr = "";

	for (int i = 0; i < (int)infoStr.size(); i++) {
		addStr += infoStr[i].c_str();
	}
	manip_t["info"] = addStr.c_str();
	rb->closeDB(); //关闭与数据库的连接
	return manip_t;
}

Json::Value DBFacade::getStatement(time_t startTime, time_t endTime) //找报表
{
	//获取报表
	RecordBean* rd = new RecordBean();
	std::vector<reqStatement*>result;
	result = rd->query3(startTime, endTime);
	Statement* sm = new Statement(startTime, endTime, result);
	

	//打包报表
	Json::Value manip_t;
	Json::StreamWriterBuilder wbuilder;
	wbuilder.settings_["indentation"] = "\t";
	manip_t["time"] = (int)sm->gettime();
	manip_t["startTime"] = (int)sm->getstartTime();
	manip_t["endTime"] = (int)sm->getendTime();
	manip_t["type"] = (int)sm->gettype();
	manip_t["responseType"] = (int)sm->getresponseType();
	manip_t["ACUseCount"] = (int)sm->getACUseCount();
	std::vector<std::string> infoStr = sm->getinfo();
	std::string addStr = "";

	for (int i = 0; i < (int)infoStr.size(); i++) {
		addStr += infoStr[i].c_str();
	}
	manip_t["info"] = addStr.c_str();
	rd->closeDB(); //关闭与数据库的连接
	return manip_t;
}

Json::Value DBFacade::getpayment(int newId) //找账单
{
	//获取账单
	RecordBean* rd = new RecordBean();
	std::vector<reqPayment*>result;
	result = rd->query1(newId);
	PaymentList* pm = new PaymentList(newId, paymentNumber++, result);

	//打包json
	Json::Value manip_t;
	Json::StreamWriterBuilder wbuilder;
	wbuilder.settings_["indentation"] = "\t";
	manip_t["time"] = (int)pm->gettime();
	manip_t["type"] = (int)pm->gettype();
	manip_t["responseType"] = (int)pm->getresponseType();
	manip_t["listId"] = (int)pm->getlistId();
	manip_t["roomId"] = (int)pm->getroomId();
	manip_t["totalFee"] = pm->gettotalFee();
	rd->closeDB(); //关闭与数据库的连接
	return manip_t;
}